<template>
  <ul class="navs">
    <li class="home">
      <RouterLink to="/">首页</RouterLink>
    </li>
    <li v-for="item in catagory" :key="item.id" @mousemove="showContent(item.id)" @mouseleave="hideContent(item.id)">
      <router-link @click="hideContent(item.id)" :to="`/category/${item.id}`">{{
        item.name
      }}</router-link>
      <div class="layer" :class="{ open: item.open }">
        <ul>
          <li v-for="i in item.children" :key="i.id">
            <router-link @click="hideContent(item.id)" :to="`/category/sub/${i.id}`">
              <img :src="i.picture" alt="" />
              <p>{{ i.name }}</p>
            </router-link>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>
<script>
  import {
    computed
  } from "@vue/reactivity";
  import {
    useStore
  } from "vuex";
  export default {
    name: "TopHeaderContent",
    setup() {
      const store = useStore();
      const catagory = computed(() => {
        return store.state.catagory.catagory;
      });
      const showContent = (id) => {
        store.commit("catagory/show", id);
      };
      const hideContent = (id) => {
        store.commit("catagory/hide", id);
      };
      return {
        catagory,
        showContent,
        hideContent
      };
    },
  };

</script>

<style lang="less" scoped>
  .navs {
    width: 820px;
    display: flex;
    justify-content: space-around;
    padding-left: 40px;

    >li {
      margin-right: 40px;
      width: 38px;
      text-align: center;
      position: relative;

      >a {
        font-size: 16px;
        line-height: 32px;
        height: 32px;
        display: inline-block;
      }

      &:hover {
        >a {
          color: @xtxColor;
          border-bottom: 1px solid @xtxColor;
        }

        // > .layer {
        //   height: 138px;
        //   opacity: 1;
        // }
      }
    }

    .layer {

      //让代码决定是否显示
      &.open {
        height: 138px;
        opacity: 1;
      }

      // width: 1240px;
      background-color: #fff;
      position: absolute;
      left: -200px;
      top: 56px;
      height: 0;
      overflow: hidden;
      opacity: 0;
      box-shadow: 0 0 5px #ccc;
      transition: all 0.2s 0.1s;
      z-index: 9999;

      ul {
        display: flex;
        // flex-wrap: wrap;
        padding: 0 70px;
        align-items: center;
        height: 138px;

        li {
          width: 110px;
          text-align: center;

          img {
            // display: inline-block;
            width: 60px;
            height: 60px;
          }

          p {
            padding-top: 10px;
          }

          &:hover {
            p {
              color: @xtxColor;
            }
          }
        }
      }
    }
  }

</style>
